<?php
class PlanController extends AppController {
    // var $helpers = array ('Html','Form');
    //errore:var $name = 'Pages'; // riferimento alla cartella delle view
    
    public function beforeFilter(){
        App::uses('CakeTime', 'Utility');
		parent::beforeFilter();
        $this->Auth->allow('index','edit','save','add','view');
		
		
    }
    
    public function index(){
         $user=$this->Auth->user();
         $plan_list="";
         if($user["Group"]["id"]==ROLE_ADMIN){
             $this->Plan->recursive=0;
             $plan_list = $this->Plan->find("all");
         }else{
            //carico l'utente docente se non c'è e lo metto in sessione
            $docente_id = $this->Session->read("ProfiloDocente.Docente.id");
            if($docente_id==null){
                $this->loadModel("Docente");
                $this->Docente->recursive=0;
                $utente = $this->Auth->user();
                $this->Session->write("ProfiloDocente",$this->Docente->findByUserId($utente["id"])); 
                $docente_id = $this->Session->read("ProfiloDocente.Docente.id");               
            }
            $this->Plan->recursive=0;
            $plan_list = $this->Plan->findAllByDocenteId($docente_id);
         }
            $this->set('plan_list',$plan_list);
    }
    
    public function view($plan_id) {
            $this->set('plan',$this->Plan->read(null,$plan_id));
    }
	
     public function edit($detail_id)
	{
	   $this->loadModel("PlanDetail");
       $detail_model = $this->PlanDetail->read(null,$detail_id);
       
       $this->set("planDetail",$detail_model);
		
	}
    
    
    public function save(){
       $this->loadModel("PlanDetail");
       if($this->PlanDetail->save($this->request->data)){
           $this->Session->setFlash(__("Dettaglio Salvato con successo"));
       }else{
           $this->Session->setFlash(__("Errore nel salvataggio del dettaglio del Piano"));
           $this->redirect($this->referer());
       }
       $this->redirect("/Plan/index");
    }
    
    
    public function add($parentId){
        $this->loadModel("PlanDetail");
        $this->PlanDetail->create();
        $this->PlanDetail->set("plan_id",$parentId);
        $this->PlanDetail->set("id",null);
        $this->set("planDetail", $this->PlanDetail->data);
       
        
    }
    
    public function lock($idPlan){
        $this->Plan->read(null,$idPlan);
        $this->Plan->set('lock', 1);
        $this->Plan->set('dt_lock', date("Y/m/d H:i:s"));
        if($this->Plan->save()){
           $this->Session->setFlash(__("Piano Bloccato"));
        }else {
           $this->Session->setFlash(__("Errore nel salvataggio del dettaglio del Piano"));
        }
            $this->redirect($this->referer());
    }
    
}
?>